package org.apache.mina.protocol.dns;

import java.net.InetAddress;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * A DnsRequest object represents an active DNS lookup.  
 * 
 * @author The Apache MINA Project (dev@mina.apache.org)
 * @version $Rev: 84 $, $Date: 2008-02-22 16:33:48 +0000 (Fri, 22 Feb 2008) $
 */
/**
 * @author optenet
 *
 */
class DnsRequest {

    /**
     * The DNS transaction ID used for the request
     */
    private int transactionId;

    /**
     * The domain name which is being resolved
     */
    private String host;

    /**
     * Number of retries
     */
    private AtomicInteger retries = new AtomicInteger(0);

    /**
     * Time of last retry
     */
    private volatile long lastRequestedTime;

    /**
     * The {@link DnsFuture} associated with this request.
     */
    private DefaultDnsFuture future;

    /**
     * Constructs a new DNS request.
     * 
     * @param transactionId the (unique) transaction ID 
     * @param host the host name is being resolved
     */
    public DnsRequest(int transactionId, String host) {
        this.transactionId = transactionId;
        this.host = host;
        this.future = new DefaultDnsFuture(host);
        this.lastRequestedTime = System.currentTimeMillis();
    }

    /**
     * @return 
     */
    public int getTransactionId() {
        return transactionId;
    }

    public String getHost() {
        return host;
    }

    public DefaultDnsFuture getFuture() {
        return future;
    }

    public int getAndSetRetries() {
        return retries.getAndDecrement();
    }

    public long getLastRequestedTime() {
        return lastRequestedTime;
    }

    /**
     * Sets the time of last DNS retry
     * 
     * @param lastRequestedTime the time (in ms).
     */
    public void setLastRequestedTime(long lastRequestedTime) {
        this.lastRequestedTime = lastRequestedTime;
    }

    /**
     * 
     * 
     * @param addresses
     */
    public void done(List<InetAddress> addresses) {
        future.setValue(addresses);
    }

    /**
     * 
     */
    public void nameError() {
        future.setNameError();
    }

}
